//接受订单
import axios from "axios";
import {dictionary} from "../utils/allDictionary";

let mixin = {
  methods:{
    format(kind,type){
      if(type === 0){
        for(let i=0 ; i<dictionary.serviceKind.length; i++)
        {
          if(dictionary.serviceKind[i].key === kind){
            return dictionary.serviceKind[i].name
          }
        }
      }

      if(type === 1)
      {
        for(let i=0 ; i<dictionary.orderStatus.length; i++)
        {
          if(dictionary.orderStatus[i].key === kind){
            return dictionary.orderStatus[i].name
          }
        }
      }

      if(type === 2){
        for(let i=0 ; i<dictionary.acceptStatus.length; i++)
        {
          if(dictionary.acceptStatus[i].key === kind){
            return dictionary.acceptStatus[i].name
          }
        }
      }

      if(type === 3){
        for(let i=0 ; i<dictionary.payStatus.length; i++)
        {
          if(dictionary.payStatus[i].key === kind){
            return dictionary.payStatus[i].name
          }
        }
      }
    },
    accept(or_id)
    {
      this.$confirm('确定接受此订单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = 'https://haotry.top/api/serviceSide/acceptAssignedOrder'
        axios.post(url, JSON.stringify({ser_id: this.ser_id, or_id}), {
          headers: {
            'Content-Type': 'application/json'
          }
        }).then(res => {
          let data = res.data
          console.log(data)
          if (data.status === 200) {
            this.$message({
              type: 'success',
              message: '订单接受成功!'
            });
            this.reload()
          } else {
            this.$message({
              type: 'info',
              message: '订单接受失败!'
            })
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      })
    },

    //拒绝订单
    reject(or_id)
    {
      this.$confirm('确定拒绝此订单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = 'https://haotry.top/api/serviceSide/rejectAssignedOrder'
        axios.post(url,JSON.stringify({ser_id:this.ser_id,or_id}),{
          headers:{
            'Content-Type':'application/json'
          }
        }).then(res => {
          let data = res.data
          console.log(data)
          if(data.status === 200){
            this.$message({
              type: 'success',
              message: '订单拒绝成功!'
            });
            this.reload()
          }
          else{
            this.$message({
              type: 'info',
              message: '订单拒绝失败!'
            })
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    handleSizeChange(val) {
      this.pageSize = val
    },
    handleCurrentChange(val) {
      this.currentPage = val
    },

    handleOrder(id){
      this.$router.push({
        name:'orderDetail',
        params:{or_id : id}
      })
    }
  }
}

export default mixin